package com.example.photography.adapter.home_recommed;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.adapter.MyBaseAdapter;
import com.example.photography.bean.ArticleBean2;
import com.example.photography.bean.UrlBean;
import com.example.photography.ui.CircleImageView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ActicleAdapter extends MyBaseAdapter {
    private List<UrlBean> urlBeans = new ArrayList<>();

    public ActicleAdapter(List list, Context context) {
        super(list, context);
    }

    @Override
    public View myGetView(int position, View convertView) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_home_article, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //装载数据
        ArticleBean2 articleBean = (ArticleBean2) list.get(position);
        //glide头像加载实现
        assert viewHolder != null;
        Glide.with(mContext)
                .load(articleBean.getHead_url())
                .thumbnail(Glide.with(mContext).load(R.drawable.head_empty))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(viewHolder.ivHead);
        viewHolder.tvName.setText(articleBean.getName());
        urlBeans = JSONObject.parseArray(articleBean.getImage_url(), UrlBean.class);
        viewHolder.svImage.setImageURI(Uri.parse(urlBeans.get(0).getUrl()));
//        @SuppressLint("SimpleDateFormat")
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date date = simpleDateFormat.parse(articleBean.getDate());
//        viewHolder.tvDate.setText(simpleDateFormat.format(date));
        viewHolder.tvDate.setText(articleBean.getDate().substring(0, 9));
        viewHolder.tvContent.setText(articleBean.getContent());
        return convertView;
    }


    static
    class ViewHolder {
        @BindView(R.id.iv_head)
        CircleImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.focus)
        TextView focus;
        @BindView(R.id.sv_image)
        SimpleDraweeView svImage;
        @BindView(R.id.tv_tag)
        TextView tvTag;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.iv_like)
        ImageView ivLike;
        @BindView(R.id.iv_comment)
        ImageView ivComment;
        @BindView(R.id.iv_collect)
        ImageView ivCollect;
        @BindView(R.id.iv_share)
        ImageView ivShare;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.ll_content)
        LinearLayout llContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
